<template>
	<view class="updatePass">
		<u-form labelPosition='top' labelWidth='120px' :model="form" :rules="rules" ref="uform">
			<u-form-item label="输入新密码" prop="newPass">
				<u-input  v-model="form.newPass" placeholder="请输入6-20位新密码" type="password"></u-input>
			</u-form-item>
			<u-form-item label="确认新密码" prop="agnitPass">
				<u-input v-model="form.agnitPass" placeholder="请再次输入6-20位新密码" type="password"></u-input>
			</u-form-item>
			<u-form-item >
				<text>密码必须是6-20个英文字母、数字或符号(除空格)，且字母、数字和标点符号至少包含两种</text>
			</u-form-item>
			<u-form-item >
				<u-button color="#fd784f" shape="circle" @click="update()">确认</u-button>
			</u-form-item>
		</u-form>
	</view>
</template>

<script>
	import {mapGetters, mapActions} from 'vuex';
	export default {
		data() {
			var validate = (rule, value, callback) => {
			        if (value === '') {
			          callback(new Error('不能为空'));
			        } else {
						let parent =/^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$).{6,20}$/;
						if(!parent.test(value)){
							callback(new Error('长度在6到20为字符和数字'));
						}else{
							callback();
						} 
			        }
			      };
				var checkPass = (rule, value, callback) => {
				        if (value === '') {
				          callback(new Error('不能为空'));
				        } else {
							if(value != this.form.newPass){
								callback(new Error('密码不一致'));
							}else{
								callback();
							}
				        }
				      };  
			return {
				form:{
					newPass:'',
					agnitPass:''
				},
				rules:{
					newPass:[{trigger: 'blur', validator:validate}],
					agnitPass:[{validator:checkPass, trigger: 'blur'}]
				}
			}
		},
		computed:{
			...mapGetters('user',['getUserList','getUser'])
		},
		methods: {
			...mapActions('user',['logOut','updateUser']),
			update(){
				this.$refs.uform.validate().then(res => {
					this.updateUser({
						userName:this.getUser.userName,
						userNumber:this.getUser.userNumber,
						password:this.form.newPass,
					})
					uni.$u.toast('修改成功请重新登录');
					this.logOut();
				setTimeout(()=>{
					uni.navigateTo({
						url:'/pagesG/logn/logn'
					})
				},500)
					this.form = {};
					}).catch(errors => {
						console.log(errors);
						uni.$u.toast('修改失败')			
					})
			}
		}
	}
</script>

<style lang="scss">
	.updatePass{
		padding: 20px;
		
	}
	.updatePass text{
		font-size: 12px;
		color: gray;
	}
</style>
